package com.his.struts.action;

import java.sql.ResultSet;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.his.struts.dao.answer.AnswerDao;
import com.his.struts.dao.answer.AnswerImp;
import com.his.struts.form.AnswerForm;
import com.his.struts.form.DoctorForm;
import com.his.struts.form.HospitalInfoForm;
import com.his.struts.form.QuestionForm;
import com.his.struts.util.MailUtil;
import com.his.struts.util.PageUtil;
import com.his.struts.util.TranCharset;

/** 
 * MyEclipse 肖伟亮
 * Creation date: 05-28-2011
 * 
 * 
 */
public class AnswerAction extends DispatchAction {
	
	AnswerDao dao = new AnswerImp();
	
	/** 
	 * Method lookQuestion 查看问题
	 * @param mapping 配置文件
	 * @param form 表单数据
	 * @param request 网页请求
	 * @param response 响应
	 * @return ActionForward 跳转到查看问题页	
	 */
	public ActionForward lookQuestion(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		AnswerForm answerForm = (AnswerForm) form;
		// 得到问题id
		int qid = answerForm.getA_qid();
		// 根据问题id 得到将要回答的问题
		QuestionForm question = dao.answer(qid);
		// 把该问题 设置到request
		request.setAttribute("question", question);
		
		String sql = "answer where a_qid = "+ qid +" order by a_qid desc";
		// 调用分页类 
		ResultSet rs = PageUtil.getPage(sql, 3, request);
		if(rs != null){
			// 封装
			List<AnswerForm> answer = dao.getAnswerBean(rs);
			// 数据放到request中
			request.setAttribute("answer", answer);
		}
		
		// 跳转到查看问题页		
		return mapping.findForward("lookQuestion");
	}

	/** 
	 * Method addAnswer 添加回答
	 * @param mapping 配置文件
	 * @param form 表单数据
	 * @param request 网页请求
	 * @param response 响应
	 * @return ActionForward null
	 */
	public ActionForward addAnswer(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		AnswerForm answerForm = (AnswerForm) form;
		HttpSession session = request.getSession();
		
		DoctorForm doc = (DoctorForm)session.getAttribute("docinfo");
		/*
		 * 如果医生不存在 则跳到错误页
		 */
		if(doc == null){
			return mapping.findForward("login");
		}
		
		// 得到医生名字
		String doctor = doc.getDocname();
		// 得到问题id
		int qid = answerForm.getA_qid();
		// 得到回答内容
		String answer = answerForm.getA_answer();
		answer = TranCharset.TranEncodeTOUTF_8(answer);
		// 给该问题添加回答
		boolean ok = dao.addAnswer(qid, answer,doctor);
		 if(ok){
			// 得到医院的信息
			 HospitalInfoForm his = (HospitalInfoForm)this.getServlet().getServletContext().getAttribute("his");
			 // 得到医院地址
			 String url = request.getServerName()+":"+request.getServerPort()+request.getContextPath();
			 String hospitalName = his.getHospitalName();
			 String message = dao.getQuestion(qid);
			 String[] mess = message.split("-"); 
			 String info = "尊敬的 <span style='color:red'>" + mess[0] +"</span> 您好， 您在<a href='http://"+url+"'>"
			 			+ hospitalName + "</a>的提问<span style='color:red'>" + mess[1] + "</span>已有新的回答，请尽早<a href='http://"+url+"'>查看</a>";
			 MailUtil.sendMail(mess[2], hospitalName, info);
		 }
		return null;
	}
	
	/** 
	 * Method questionMy 查看提问我的
	 * @param mapping 配置文件
	 * @param form 表单数据
	 * @param request 网页请求
	 * @param response 响应
	 * @return ActionForward 跳转到提问我的页面
	 */
	public ActionForward questionMy(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		HttpSession session = request.getSession();
		DoctorForm doc = (DoctorForm)session.getAttribute("docinfo");
		/*
		 * 如果医生不存在 则跳到错误页
		 */
		if(doc == null){
			return mapping.findForward("login");
		}
		// 得到医生名字 、主治专科
		String zhuanke = doc.getDocattend(); // 主治专科
		String doctor = doc.getDocname();	// 医生名字
		
		String sql = "questions where qtype ='"+ zhuanke +"' and qid not in " +
					"(select a_qid from answer where a_doctor = '"+ doctor +"') order by qid desc";
		// 调用分页类 
		ResultSet rs = PageUtil.getPage(sql, 10, request);
		// 如果数据不为空
		if(rs != null){
			// 封装
			List<QuestionForm> question = dao.getMyQuestions(rs);
			// 数据放到request中
			request.setAttribute("QuestionMy", question);
		}
		
		// 跳转到提问我的页面
		return mapping.findForward("questionMy");
	}
	
	/** 
	 * Method myAnswer 查看我所有回答过到问题
	 * @param mapping 配置文件
	 * @param form 表单数据
	 * @param request 网页请求
	 * @param response 响应
	 * @return ActionForward 我回答过的页面
	 */
	public ActionForward myAnswer(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		// 得到医生名字
		String doctor = (String)request.getSession().getAttribute("ysname");
		/*
		 * 如果医生不存在 则跳到错误页
		 */
		if(doctor == null){
			return mapping.findForward("login");
		}
		
		String sql = "questions where qid in " +
		"(select a_qid from answer where a_doctor = '"+ doctor +"') order by qid desc";
		// 调用分页类 
		ResultSet rs = PageUtil.getPage(sql, 10, request);
		if(rs != null){
			// 封装
			List<QuestionForm> question = dao.getMyQuestions(rs);
			// 数据放到request中
			request.setAttribute("question", question);
		}
		
		// 跳转到我回答过的页面
		return mapping.findForward("myAnswer");
	}
	
}